package com.kim.smartring.activity;

import java.util.UUID;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.kim.smartring.R;
import com.kim.smartring.db.dao.StepRecordDao;
import com.kim.smartring.db.dao.UserDao;
import com.kim.smartring.db.model.StepRecord;
import com.kim.smartring.db.model.User;
import com.kim.smartring.location.LocationUtils;
import com.kim.smartring.location.LocationUtils.OnLocationReceiver;
import com.kim.smartring.net.business.LoginBusiness;
import com.kim.smartring.net.business.LoginBusiness.LoginCallback;
import com.kim.smartring.net.business.RegisterBusiness;
import com.kim.smartring.net.business.RegisterBusiness.RegisterCallback;
import com.kim.smartring.net.business.SyncBusiness;
import com.kim.smartring.net.business.SyncBusiness.SyncCallback;
import com.kim.smartring.net.business.UpdateBindingBusiness;
import com.kim.smartring.net.business.UpdateBindingBusiness.UpdateBindingCallback;
import com.kim.smartring.net.business.UpdateUserInfoBusiness;
import com.kim.smartring.net.business.UpdateUserInfoBusiness.UpdateUserInfoCallback;
import com.kim.smartring.net.entity.SyncEntity.SyncType;

public class MainActivity extends Activity {

	private String TAG = "MainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		// testDB();
		// testAddUser();
		// testLocation();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void initView() {
		setContentView(R.layout.activity_main);
		findViewById(R.id.btn_scan_demo).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						testDemoScanDevices();
					}
				});
		findViewById(R.id.btn_scan).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Intent intent = new Intent(MainActivity.this,
				// DeviceActivity.class);
				// startActivityForResult(intent, 0);
				testScanDevices();
			}
		});

		findViewById(R.id.btn_register).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						testAddUser();
					}
				});

		findViewById(R.id.btn_update_user).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						testUpdateUserInfo();
					}
				});
		findViewById(R.id.btn_login).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				testLogin();

			}
		});
		findViewById(R.id.btn_update_binding).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						testUpdateBinding();
					}
				});
		findViewById(R.id.btn_sync_step).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						testSyncStepRecord();
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/*
		 * if (RESULT_OK == resultCode) { String address = data
		 * .getStringExtra(BluetoothState.EXTRA_DEVICE_ADDRESS); Log.d(TAG,
		 * String.format("address:%s", address));
		 * SmartRingApplication.getBluetoothUtil().connect(address);
		 * SmartRingApplication.getBluetoothUtil()
		 * .setBluetoothConnectionListener( new BluetoothConnectionListener() {
		 * 
		 * @Override public void onDeviceDisconnected() { Log.d(TAG,
		 * "onDeviceDisconnected"); }
		 * 
		 * @Override public void onDeviceConnectionFailed() { Log.d(TAG,
		 * "onDeviceConnectionFailed"); }
		 * 
		 * @Override public void onDeviceConnected(String name, String address)
		 * { Log.d(TAG, String.format(
		 * "connected device Name:%s | connected device address:%s", name,
		 * address)); } }); }
		 */
	}

	private void testDemoScanDevices() {
		Intent intent = new Intent();
		intent.setClass(this, DemoDeviceActivity.class);
		startActivity(intent);
	}

	private void testScanDevices() {
		Intent intent = new Intent();
		intent.setClass(this, DeviceActivity.class);
		startActivity(intent);
	}

	private void testDB() {
		User user = new User();
		user.setAccount("kim");
		user.setPassword("1234567890");
		user.setAge(25);
		user.setGender(1);
		user.setHeight(168);
		user.setWeight(76);

		UserDao dao = new UserDao(this);
		dao.save(user);
		dao = null;

		StepRecord record = new StepRecord();
		record.setDay("30");
		record.setsCalories(168);
		record.setsCount(1890);
		record.setsDistance(0.6f);
		record.setSid(UUID.randomUUID().toString());
		record.setsSyncState(0);
		record.setsTimeStart(System.currentTimeMillis());
		record.setsTimeEnd(System.currentTimeMillis());
		record.setYearMonth("201405");

		StepRecordDao stepRecordDao = new StepRecordDao(this);
		stepRecordDao.save(record);
		stepRecordDao = null;
	}

	/**
	 * 测试位置定位
	 */
	private void testLocation() {
		LocationUtils locationUtils = LocationUtils.getInstance();
		locationUtils.doLocation(this, new OnLocationReceiver() {

			@Override
			public void onReceiver(boolean result) {

			}
		});
	}

	private void testAddUser() {
		User user = new User();
		user.setAccount("kim");
		user.setPassword("1234567890");
		user.setAge(25);
		user.setGender(1);
		user.setHeight(168);
		user.setWeight(76);
		user.setUserAddress("深圳");
		user.setDeviceAddress("67:s8:23:54:d9");

		RegisterBusiness.register(this, user, new RegisterCallback() {

			@Override
			public void registerSuccess() {

			}

			@Override
			public void registerFail() {

			}
		});
	}

	private void testUpdateUserInfo() {
		User user = new User();
		user.setUserId("1401526122411"); // // userId=1401524732426
											// userId=1401526122411
		user.setHeight(170);
		user.setWeight(66);
		user.setUserAddress("深圳");
		user.setDeviceAddress("67:s8:23:54:a8");
		UpdateUserInfoBusiness.updateUserInfo(this, user,
				new UpdateUserInfoCallback() {

					@Override
					public void updateSuccess() {
						Toast.makeText(getApplicationContext(), "更新用户信息成功",
								Toast.LENGTH_SHORT).show();
					}

					@Override
					public void updateFail() {
						Toast.makeText(getApplicationContext(), "更新用户信息失败",
								Toast.LENGTH_SHORT).show();
					}
				});
	}

	private void testLogin() {
		User user = new User();
		user.setAccount("kim");
		user.setPassword("1234567890");
		LoginBusiness.login(this, user, new LoginCallback() {

			@Override
			public void loginSuccess() {
				Toast.makeText(getApplicationContext(), "登录成功",
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void loginFail() {
				Toast.makeText(getApplicationContext(), "登录失败",
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void testUpdateBinding() {
		String uid = "1401526122411";
		String deviceId = "67:s8:23:54:1x";
		UpdateBindingBusiness.updateBinding(this, uid, deviceId,
				new UpdateBindingCallback() {

					@Override
					public void onUpdateSuccess() {
						Toast.makeText(getApplicationContext(), "更新绑定成功",
								Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onUpdateFail() {
						Toast.makeText(getApplicationContext(), "更新绑定失败",
								Toast.LENGTH_SHORT).show();
					}
				});
	}

	private void testSyncStepRecord() {
		SyncBusiness.syncStepInfo(this, new SyncCallback() {

			@Override
			public void onSyncSuccess(SyncType syncType) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSyncFail(SyncType syncType) {
				// TODO Auto-generated method stub

			}
		});
	}
}
